<template>
  <el-form
    ref="ruleForm"
    :model="ruleForm"
    :rules="rules"
    label-position="left"
    label-width="0px"
    class="demo-ruleForm login-container">
    <h3 class="title">登录您的帐户</h3>
    <el-form-item prop="account">
      <el-input v-model="ruleForm.account" type="text" auto-complete="off" placeholder="请输入账号" />
    </el-form-item>
    <el-form-item prop="checkPass">
      <el-input v-model="ruleForm.checkPass" type="password" auto-complete="off" placeholder="请输入密码" />
    </el-form-item>
    <el-checkbox v-model="checked" checked class="remember">记住密码</el-checkbox>
    <el-form-item style="width: 100%;">
      <el-button type="primary" style="width: 100%;" :loading="loading" @click.native.prevent="handleSubmit">登录
      </el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import { loginAPI } from '@/api/login'

export default {
  data() {
    return {
      checked: true,
      loading: false,
      ruleForm: {
        account: '18570142913',
        checkPass: '72crm@'
      },
      rules: {
        account: [
          { required: true, message: '账号不能为空', trigger: 'blur' }
          // { validator: validaePass }
        ],
        checkPass: [
          { required: true, message: '密码不能为空', trigger: 'blur' }
          // { validator: validaePass2 }
        ]
      }
    }
  },
  methods: {
    handleSubmit(ev) {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          this.loading = true
          loginAPI({
            username: this.ruleForm.account,
            password: this.ruleForm.checkPass
          }
          ).then(res => {
            if (res.code === 0) {
              this.$router.push({ path: '/' })
            } else {
              this.loading = false
            }
          })
        } else {
          console.log('error submit!!')
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.login-container {
  width: 350px;
  padding: 35px 35px 15px;
  margin: 220px auto;
  background: #fff;
  background-clip: padding-box;
  border: 1px solid #eaeaea;
  -webkit-border-radius: 5px;
  -moz-border-radius: 5px;
  border-radius: 5px;
  box-shadow: 0 0 25px #cac6c6;

  .title {
    margin: 0 auto 40px;
    color: #505458;
    text-align: center;
  }

  .remember {
    margin: 0 0 35px;
  }
}
</style>
